package org.itboys.mobile.service.mysql.weixin;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.dto.product.SundayProductCategoryDto;
import org.itboys.mobile.entity.mysql.SundayProductCategory;
import org.itboys.mysql.service.BaseMySqlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_商品_分类_service_weixin
 */
@Service
public class SundayWeiXinProductCategoryService extends BaseMySqlService<SundayProductCategory> {

    @Autowired
    private ResourceHolder resourceHolder;
    public List<SundayProductCategoryDto> selectCategory(){
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("sort_name","sequence");
        param.put("sort_order","asc");
        return packageMuilt(super.select(param));
    }
    public SundayProductCategory findOne(Long categoryId){
        SundayProductCategory category=super.find(categoryId);
        return category;
    }

    /**
     * 获取商品类目的类目。树结构类型。
     */
    public List<SundayProductCategoryDto> getTree(Long memberId, Long categoryId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("sort", "sequence");
        param.put("order", "asc");
        List<SundayProductCategoryDto> alls = packageMuilt(super.select(param));
        List<SundayProductCategoryDto> parents = new ArrayList<SundayProductCategoryDto>();
        List<SundayProductCategoryDto> children = new ArrayList<SundayProductCategoryDto>();

        for (SundayProductCategoryDto all : alls) {
            if (StringUtils.isNotEmpty(all.getImage())) {
                all.setImage(resourceHolder.getStringValue("imgRoot") + all.getImage());
            } else {
                all.setImage(null);
            }

            if (all.getParentId().longValue() == 0l) {

                parents.add(all);
            } else {
                children.add(all);
            }
        }
        //组装TREE
        for (SundayProductCategoryDto parent : parents) {
            List<SundayProductCategoryDto> child = new ArrayList<SundayProductCategoryDto>();
            for (SundayProductCategoryDto c : children) {
                if (LongUtil.isNotZreo(c.getParentId()) &&
                        c.getParentId().longValue() == parent.getId()) {
                    child.add(c);
                }
            }
            parent.setChildren(child);
        }
        return parents;
    }

    public SundayProductCategoryDto packageSingle(SundayProductCategory productCategory) {
        String imageRoot = resourceHolder.getStringValue("imgRoot");
        return new SundayProductCategoryDto(productCategory.getId(), productCategory.getCreateTime(), productCategory.getCreateDate(),
                imageRoot+productCategory.getImage(), productCategory.getName(), productCategory.getParentId());
    }

    public List<SundayProductCategoryDto> packageMuilt(List<SundayProductCategory> productCategories) {
        List<SundayProductCategoryDto> dtos = new ArrayList<SundayProductCategoryDto>();
        for (SundayProductCategory productCategory : productCategories) {
            dtos.add(packageSingle(productCategory));
        }
        return dtos;

    }
}
